package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    // 通过宠物id找到宠物详情
    @Override
        public PetDetail findDetailByPetId(Long id) {
        PetDetail petDetail = petDetailMapper.loadByPetId(id);

        return petDetail;
    }

    @Override
    public void onOrOffsale(int i, List<Long> ids) {
        // 1是上架 0是下架
        if (i == 1) {
            HashMap<String, Object> params  = new HashMap<>();
            params.put("onsaleTime",new Date());
            params.put("ids",ids);
            petMapper.onsale(params);
        } else if (i == 0){
            HashMap<String, Object> params  = new HashMap<>();
            params.put("offsaleTime",new Date());
            params.put("ids",ids);
            petMapper.offSale(params);
        }

    }

    // 下单 传入参数商品 id 用户id1
    @Override
    public void adopt(Long id, Long loginInfoId) {
        // 1.修改宠物状态下架 下架时间
        Pet pet = petMapper.loadById(id);
        if(pet != null){
            pet.setState(0);
            pet.setOffsaletime(new Date());
            // 2.绑定领养人
            User user = userMapper.loadByLoginInfoId(loginInfoId);
            pet.setUser_id(user.getId());
            petMapper.update(pet);
            //3.@TODO 生成订单并支付

        }
    }

    @Override
    public Pet loadByIdDetail(Long id) {

        return petMapper.loadByIdDetail(id);
    }
}
